﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.Settlements.Alley
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System.Collections.Generic;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.Settlements
{
  public class Alley : SettlementArea
  {
    private Settlement _settlement;
    [CachedData]
    private TextObject _name;
    [SaveableField(10)]
    private Hero _owner;
    private string _tag;

    internal static void AutoGeneratedStaticCollectObjectsAlley(
      object o,
      List<object> collectedObjects)
    {
      ((SettlementArea) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this._owner);
    }

    internal static object AutoGeneratedGetMemberValue_owner(object o)
    {
      return (object) ((Alley) o)._owner;
    }

    public override Settlement Settlement => this._settlement;

    public override TextObject Name => this._name;

    public override Hero Owner => this._owner;

    public override string Tag => this._tag;

    public void SetOwner(Hero newOwner)
    {
      if (this._owner != null)
        this._owner.OwnedAlleys.Remove(this);
      Hero owner = this._owner;
      this._owner = newOwner;
      if (this._owner != null)
      {
        this._owner.OwnedAlleys.Add(this);
        this.State = this._owner == Hero.MainHero ? Alley.AreaState.OccupiedByPlayer : Alley.AreaState.OccupiedByGangLeader;
      }
      else
        this.State = Alley.AreaState.Empty;
      CampaignEventDispatcher.Instance.OnAlleyOwnerChanged(this, newOwner, owner);
    }

    public Alley.AreaState State { get; private set; }

    public Alley(Settlement settlement, string tag, TextObject name)
    {
      this.Initialize(settlement, tag, name);
    }

    public void Initialize(Settlement settlement, string tag, TextObject name)
    {
      this._name = name;
      this._settlement = settlement;
      this._tag = tag;
    }

    internal void AfterLoad()
    {
      if (this._owner != null)
      {
        this.State = this._owner == Hero.MainHero ? Alley.AreaState.OccupiedByPlayer : Alley.AreaState.OccupiedByGangLeader;
        this._owner.OwnedAlleys.Add(this);
        if (!(MBSaveLoad.LastLoadedGameVersion < ApplicationVersion.FromString("v1.2.0")) || this._owner.IsAlive)
          return;
        this.SetOwner((Hero) null);
        this.State = Alley.AreaState.Empty;
      }
      else
        this.State = Alley.AreaState.Empty;
    }

    public enum AreaState
    {
      Empty,
      OccupiedByGangLeader,
      OccupiedByPlayer,
    }
  }
}
